package com.bridge.client.bridgedynamcclient.controller;

import com.bridge.client.bridgedynamcclient.bean.ChoiceItem;
import com.bridge.client.bridgedynamcclient.custom_compinents.TextImageView;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.util.Callback;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

public class BridgeImageSampleController extends BaseController implements Initializable {

    TextImageView textImageView=new TextImageView(800,800);
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        logger.info("initialize--"+this.getClass().getSimpleName());
        initListener();
    }

    @Override
    public void setRoot(AnchorPane root) {
        super.setRoot(root);
        initView();
    }

    private void initView() {
        AnchorPane.setTopAnchor(textImageView,0.0);
        root.getChildren().add(textImageView);
    }


    private void initListener() {
//        lvImageSample.setCellFactory(new Callback<ListView<ChoiceItem>, ListCell<ChoiceItem>>() {
//            @Override
//            public ListCell<ChoiceItem> call(ListView<ChoiceItem> choiceItemListView) {
//                return new ImageSampleListCell();
//            }
//        });
//        lvImageSample.setEditable(false);
//        lvImageSample.setItems(FXCollections.observableArrayList());
    }

    private static class ImageSampleListCell extends ListCell<ChoiceItem>{
        @Override
        protected void updateItem(ChoiceItem choiceItem, boolean b) {
            super.updateItem(choiceItem, b);
            if(Objects.nonNull(choiceItem)){
                VBox vBox=new VBox();
                Text text=new Text(choiceItem.getName());
                ImageView image=new ImageView();
                URL resource = this.getClass().getResource(choiceItem.getValue());
                image.setImage(new Image(resource.toString()));
                vBox.getChildren().addAll(text,image);
                setGraphic(vBox);

            }else {
                setGraphic(null);
            }

        }
    }

    public void setConfig(List<ChoiceItem> lists) {
        logger.info("setConfig");
        if(lists.isEmpty()){
            return;
        }
        List<TextImageView.ImageData> imageDataList=new ArrayList<>();
        for (ChoiceItem item : lists) {
            imageDataList.add(new TextImageView.ImageData(item.getName(),item.getValue(),1));
        }
        textImageView.setImages(imageDataList,500,500);
       // lvImageSample.getItems().addAll(lists);
    }
}
